var obj;
function Table(){
    this.index=1;
    this.dept='1002O110000000000MML';   //pmc部门
    this.year="";
    this.month="";
    this.url="";
    this.queryParams={};
    this.height=document.getElementById("section").clientHeight;
    this.tableInit=function () {
        $('#dg').edatagrid({
            width:'100%',
            height:this.height,
            url:this.url,
            queryParams:this.queryParams,
            showFooter:true,
            autoSave:true,
            columns:[[
                {"field": 'empName', "title": '姓名', "width": '5%', "align": "center","editor": {"type": "text"}},
                {"field": 'empCode', "title": '工号', "width": '5%', "align": "center","editor": {"type": "text"}},
                {"field": 'postName', "title": '职务', "width": '8%', "align": "center","editor": {"type": "combobox","options": {onChange:postNameOnChange,editable:false,valueField:"v",textField:"t",
                            panelHeight:'auto',data:[{v:'排产员',t:'排产员'},{v:'排产员主管',t:'排产员主管'},{v:'生管员',t:'生管员'},{v:'生管员主管',t:'生管员主管'}]}}},
                {"field": 'assessDept', "title": '选择部门', "width": '8%', "align": "center","editor": {"type": "combobox","options": {onChange:assessDeptOnChange,editable:false,valueField:"v",textField:"t",
                            panelHeight:'auto',data:[{v:'包装车间',t:'包装车间'},{v:'配制车间',t:'配制车间'},{v:"",t:""}],
                        }}},
                {"field": 'produce',"title": '生产效率（B）',"width": '5%',"align": "center",
                    "editor": {"type": "textbox","options": {onChange:producePrecisionOnChange}}},
                {"field": 'producePrecision',"title": '生产计划下<br>达准时率A5<br>(权重60%)',"width": '5%',"align": "center",
                    "editor": {"type": "textbox", "options": {onChange:calculatePersonScore}}},
                {"field": 'bzPrecision',"title": '包装车间计<br>划准确率A6<br>(权重40%)',"width": '5%',"align": "center",
                    "editor": {"type": "textbox", "options": {onChange:producePrecisionOnChange}}},
                {"field": 'pzPrecision',"title": '配制车间计<br>划准确率A7<br>(权重40%)',"width": '5%',"align": "center",
                    "editor": {"type": "textbox", "options": {onChange:producePrecisionOnChange}}},
                {"field": 'sgPrecision',"title": '生管员工作<br>准确率A2<br>(权重50%)',"width": '5%',"align": "center",
                    "editor": {"type": "textbox", "options": {onChange:producePrecisionOnChange}}},
                {"field": 'sgScore',"title": '生管员工作<br>量得分A3<br>(权重30%)',"width": '5%',"align": "center",
                    "editor": {"type": "textbox", "options": {onChange:calculatePersonScore}}},
                {"field": 'orders',"title": '订单依期准<br>时交付A4<br>(权重20%)',"width": '5%',"align": "center",
                    "editor": {"type": "textbox", "options": {onChange:calculatePersonScore}}},
                {"field": 'personScore',"title": '个人当月得<br>分',"width": '5%',"align": "center",
                    "editor": {"type": "textbox", "options": {}}},
                {"field": 'performanceBonusNorm', "title": '综合奖金基<br>数', "width": '5%',"align": "center","editor": {"type": "textbox", "options": {onChange:resultMoneyOnChange}}},
                {"field": 'attendanceRate', "title": '出勤率', "width": '5%',"align": "center","editor": {"type": "textbox", "options": {onChange:resultMoneyOnChange}}},
                {"field": 'otherAssess', "title": '其他考核', "width": '5%',"align": "center","editor": {"type": "textbox", "options": {onChange:resultMoneyOnChange}}},
                {"field": 'resultMoney', "title": '综合<br/>绩效（元）', "width": '8%',"align": "center","editor": {"type": "textbox", "options": {"readonly":true}}},
                {"field": 'remarks', "title": '备注',"width": '11%', "align": "center","editor": {"type":"textarea"}}
            ]],
            onLoadSuccess:data=>{
                if (data.rows.length==0){
                    windowsOperation(null,null,{status:200,msg:"没有"+obj.month+"月的绩效数据！"})
                }
            },
            onBeforeEdit:function (index, row) {
                obj.index=index;
            },
            onAfterEdit:function () {
                calculateFooter()
            }
        });
    }

    //计算页脚行
    function calculateFooter() {
        //获取所有的行
        var rows=$("#dg").datagrid("getRows");
        var resultMoney=0;
        rows.forEach(function (val) {

            var r = val.resultMoney;
            if (r){
                resultMoney+=Number(r);
            }
        })
            //统计
        //重新加载页脚行
        $('#dg').datagrid('reloadFooter',[
            {empCode: '合计', resultMoney: fomatFloat(resultMoney,2)},
        ]);

    }

    //综合<br/>绩效（元）
    function resultMoneyOnChange(){
        var performanceBonusNorm = editObj("performanceBonusNorm").textbox("getValue"); //performanceBonusNorm 综合奖金基<br>数
        performanceBonusNorm=performanceBonusNorm?Number(performanceBonusNorm):0;
        var otherAssess = editObj("otherAssess").textbox("getValue"); //出勤率
        otherAssess=otherAssess?Number(otherAssess):0;
        var attendanceRate = editObj("attendanceRate").textbox("getValue"); //出勤率
        attendanceRate=attendanceRate?Number(attendanceRate)/100:0;
        var personScore = editObj("personScore").textbox("getValue"); //个人当月得分
        personScore=Number(personScore)?personScore/100:0;
        //出勤率A1*个人当月得分*综合奖金基数     S2=X2*（A5*60%+A6*40%）*A1-F
        var result=fomatFloat(performanceBonusNorm*attendanceRate*personScore+otherAssess,2);
        var resultMoney = editObj("resultMoney"); //综合<br/>绩效（元）
        resultMoney.textbox("setValue",result)
    }

    //计算个人当月得分
    function calculatePersonScore(){
        var resltData=0;
        var postName = editObj("postName").combobox("getValue");
        if (postName.includes("排产员")) {
            //生产计划下达准时率
            var producePrecision = editObj("producePrecision").textbox("getValue")
            producePrecision= producePrecision?producePrecision:0;
            //配制装车间计划准确率
            var pzPrecision = editObj("pzPrecision").textbox("getValue")
            pzPrecision= pzPrecision?pzPrecision:0;
            //包装车间计划准确率
            var bzPrecision = editObj("bzPrecision").textbox("getValue")
            bzPrecision= bzPrecision?bzPrecision:0;
            //计算：三个相加
            resltData=fomatFloat(Number(producePrecision)+Number(pzPrecision)+Number(bzPrecision),2)
        }
        if (postName.includes("生管员")) {
            //生管员工作准确率
            var sgPrecision = editObj("sgPrecision").textbox("getValue")
            sgPrecision= sgPrecision?sgPrecision:0;
            //生管员工作量得分
            var sgScore = editObj("sgScore").textbox("getValue")
            sgScore= sgScore?sgScore:0;
            var orders = editObj("orders").textbox("getValue")
            orders= orders?orders:0;
            resltData= fomatFloat(Number(sgScore)+Number(sgPrecision)+Number(orders),2)
        }
        var personScore = editObj("personScore");
        personScore.textbox("setValue",resltData)
    }

    //计算综合奖金基数事件
    function producePrecisionOnChange(){
        //职务
        var postName = editObj("postName").combobox("getValue");
        var assessDept = editObj("assessDept").combobox("getValue");
        var produce = editObj("produce").textbox("getValue");
        //生管员工作准确率
        var sgPrecision = editObj("sgPrecision").textbox("getValue")
        //配制装车间计划准确率
        var pzPrecision = editObj("pzPrecision").textbox("getValue")
        //包装车间计划准确率
        var bzPrecision = editObj("bzPrecision").textbox("getValue")

        var resulData=0;
        if (produce>=98) {  //所有的都必须满足 生产效率大于等于98
            if (postName.includes("排产员")) {

                if (assessDept == "包装车间") {
                    resulData = otherA(bzPrecision)
                }
                if (assessDept == "配制车间") {
                    resulData= otherA(pzPrecision)
                }
            }
            if (postName.includes("生管员")) {
                //权重50
                var sgPrecision = fomatFloat( Number(sgPrecision)*(100/50),2)
                sgPrecision=sgPrecision>100?100:sgPrecision
                if (95 < sgPrecision && sgPrecision < 97) {
                    resulData=500
                } else if (97 <= sgPrecision && sgPrecision < 100){
                    resulData=600
                }else if (sgPrecision==100){
                    resulData=700
                }
            }
        }
        //调用计算个人当月得分事件
        calculatePersonScore()
        // 赋值综合奖金基<br>数
        var r = editObj("performanceBonusNorm");
        r.textbox("setValue",resulData)
    }

    //职务事件
    function postNameOnChange(newValue){
        switch (newValue) {
            case "排产员主管":
            case "排产员":
                //启用
                var dept = editObj("assessDept");
                dept.combobox("setValue","包装车间")
                dept.combobox("enable")
                editObj("producePrecision").textbox("enable")
                editObj("bzPrecision").textbox("enable")
                //禁用，并清空
                var pzPrecision= editObj("pzPrecision")
                pzPrecision.textbox("disable")
                pzPrecision.textbox("setValue","")
                var sgPrecision=editObj("sgPrecision")
                sgPrecision.textbox("disable")
                sgPrecision.textbox("setValue","")
                var sgScor=editObj("sgScore")
                sgScor.textbox("disable")
                sgScor.textbox("setValue","")
                var orders=editObj("orders")
                orders.textbox("disable")
                orders.textbox("setValue","")
                break;
            case "生管员主管":
            case "生管员":
                //启用
                editObj("sgPrecision").textbox("enable")   //生管员工作<br>准确率
                editObj("sgScore").textbox("enable")
                editObj("orders").textbox("enable")
                //禁用，并清空
                var producePrecision=editObj("producePrecision")//产计划下<br>达准时率A5
                producePrecision.textbox("disable")
                producePrecision.textbox("setValue","")
                var pzPrecision =editObj("pzPrecision")
                pzPrecision.textbox("disable")
                pzPrecision.textbox("setValue","")
                var bzPrecision= editObj("bzPrecision")
                bzPrecision.textbox("disable")
                bzPrecision.textbox("setValue","")
                var dept = editObj("assessDept");
                dept.combobox("setValue","")
                dept.combobox("disable")
                break;
        }
    }

    //部门事件
    function assessDeptOnChange(newValue) {
        switch (newValue) {
            case "包装车间":
                var pzPrecision= editObj("pzPrecision")
                pzPrecision.textbox("disable")
                pzPrecision.textbox("setValue","")
                editObj("bzPrecision").textbox("enable")
                break;
            case "配制车间":
                editObj("pzPrecision").textbox("enable")
                var bzPrecision= editObj("bzPrecision")
                bzPrecision.textbox("disable")
                bzPrecision.textbox("setValue","")
                break;
        }
    }
}

window.onload=function () {
    //为body标签赋值高度--body默认是0：获取html的高度，
    (function () {
        var htm = window.document.documentElement;
        document.body.style.height=htm.clientHeight+"px";
    })()

    obj=new Table();

    //选择时间触发事件
    document.getElementById("currentDate").addEventListener("blur",function (e) {
        var val = this.value.split("-");
        obj.year=parseInt(val[0]);
        obj.month=parseInt(val[1]);
    })

    //初始化表
    obj.tableInit();
}

function editObj(fieldName) {
    return  $('#dg').datagrid("getEditor",{index:obj.index,field:fieldName}).target;
}

//加载员工点击事件：初始化表格
function toLoadEmp(){
    obj.url="/jx/wlb/queryEmp";
    obj.queryParams={pkDept:obj.dept};
    obj.tableInit();
}

//绩效数据点击事件：初始化表格
function toLoadJxAssessWlbPmc() {
    var year=obj.year;
    var month=obj.month;
    if (!year){
        windowsOperation(null,null,{status:200,msg:"请先选择日期!"})
        return;
    }
    obj.url="/jx/wlb/queryJxAssessWlbPmc";
    //年、月、合计表中的类型（pmc部门类型：1）
    obj.queryParams={year:year,month:month,type:1};
    obj.tableInit();
}

//添加绩效数据
function addData() {
    //判断是否有选择日期
    var year = obj.year;
    if (!year){
        windowsOperation(null,null,{status:200,msg:"添加失败，请输入日期！"})
        return
    }
    var month = obj.month;
    var resultData;
    var rows = $("#dg").datagrid("getRows");
    rows.forEach(function ( val) {
        val.assessYear=year;
        val.assessMonth=month;
    })
    var footers = $("#dg").datagrid("getFooterRows");
    var jxAssessWlbTotal={};
    var jxAssessWlbTotals=[];
    footers.forEach((val)=>{
        jxAssessWlbTotal.assessYear=year;
        jxAssessWlbTotal.assessMonth=month;
        jxAssessWlbTotal.assessType=1;
        jxAssessWlbTotal.totalMoney=val.resultMoney;
        jxAssessWlbTotal.remarks=val.empCode;
        jxAssessWlbTotals.push(jxAssessWlbTotal);
    })
    resultData={"jxAssessWlbPmcs":rows,jxAssessWlbTotals:jxAssessWlbTotals}
    resultData=JSON.stringify(resultData);
   var data= commonAjax("post","/jx/wlb/addPmcData",resultData,"application/json;charset=utf-8");
   windowsOperation(null,null,data)
}

function otherA(value) {
    //95%<A6<97% 且B≥98%
    //97%<A6<100%且B≥98%
    //A6=100%且B≥98%
    var result=0;
    //权重40
    var value =fomatFloat(Number(value)*(100/40),2)
    value=value>100?100:value
    if (95 < value && value < 97) {
        result=400
    } else if (97 <= value && value < 100){
        result=500
    }else if (value==100){
        result=600
    }
    return result;
}
